package IOTWeb.com.handler;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.commons.io.IOUtils;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import IOTWeb.com.IOTMain;
import IOTWeb.com.agreementclient.WebServer;
import IOTWeb.com.utils.FileOption;
import IOTWeb.com.utils.MyExe;
import IOTWeb.com.utils.Utils;

public class MyExeHandler implements HttpHandler{

	public void handle(HttpExchange exchange) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Utils utils = new Utils();
					FileOption fileOption = new FileOption();
					//��ȡurl
					System.out.println("url:" + exchange.getRequestURI().toString());
					// ��ò�ѯ�ַ���(get)
					String queryString = exchange.getRequestURI().getQuery();
					Map<String, String> queryStringInfo = utils.formData2Dic(queryString);
					String postString = IOUtils.toString(exchange.getRequestBody());
					Map<String, String> postInfo = utils.formData2Dic(postString);
					System.out.println(postInfo);
					//��ȡresponseͷ
					Headers responseHeaders = exchange.getResponseHeaders();
					//��ȡrequestͷ
					Headers requestHeaders = exchange.getRequestHeaders();
					utils.setResponse(responseHeaders);
					OutputStream os = exchange.getResponseBody();
					PrintStream out = new PrintStream(os);
					utils.getIpLocation(exchange);
                	System.out.println("The user asked for resource: " + "myexe");
                	MyExe exe = new MyExe();
                	exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
                    out.flush();
                    out.close();
					os.close();
				} catch (IOException ie) {
					ie.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

}
